package com.amway.ll.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amway.ll.dao.EmployeeMapper;
import com.amway.ll.model.Employee;
import com.amway.ll.utils.BuildJsons;
import com.amway.ll.utils.MyConnFactory;

@Controller()
public class  EmployeeController{

	EmployeeMapper mapper = (EmployeeMapper) MyConnFactory.getMyConn().getConnection("EmployeeMapper");
	@RequestMapping("/getemployees") 
	@ResponseBody()
	public String getEmployees(Employee e){
		String s = null;
		
		//EmployeeMapper mapper = (EmployeeMapper) MyConnFactory.getMyConn().getConnection("EmployeeMapper");

		List<Employee> employees = mapper.getEmpById(e.getId());

		s = BuildJsons.getListJson(employees);
		
		return s;		
	}
	
	@RequestMapping("/getstructure") 
	//@ResponseBody()
	public String getTree(Employee e,HttpServletRequest req){
		String s = null;
		
		//EmployeeMapper mapper = (EmployeeMapper) MyConnFactory.getMyConn().getConnection("EmployeeMapper");

		List<Employee> employees = mapper.getEmpById(e.getId());
		
		
		Object[] t = employees.toArray();
		System.out.println("**********************************");
		if (t.length == 0) {
			System.out.println("没有结果");
		}
		s = "";
		for (int i = 0; i < t.length; i++) {

			e = (Employee) t[i];
			s += "myTree.add(" + e.getCode() + "," + e.getPcode() +",\'" + e.getName()  + "\');\n";
			System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+s);
		
		}
		
		req.setAttribute("a", s);
		return "tree";		
	}
	
	@RequestMapping("/inseremployees") 
	@ResponseBody()
	public String insertEmployees(Employee e){
		String s = "{success:true}";
		System.out.print(e.getArea()+e.getBirthday()+"code:::"+e.getCode()+"     "+e.getId()+e.getIdcard()+e.getName()+e.getPcode());
		mapper.insertEmp(e);
		return s;		
	}
	
	@RequestMapping("/deleteemployees") 
	@ResponseBody()
	public String deleteEmployees(Employee e){
		String s = "{success:true}";
		System.out.println(e.getCode());
		mapper.deleteEmp(e);
		return s;		
	}
	
	@RequestMapping("/updateemployees") 
	@ResponseBody()
	public String updateEmployees(Employee e){
		String s = "{success:true}";
		System.out.println("e.id"+e.getId());
		System.out.println("e.name"+e.getName());
		System.out.println("e.pcode"+e.getPcode());
		System.out.println("e.idcard"+e.getIdcard());
		System.out.println("e.area"+e.getArea());
		System.out.println("e.birthday"+e.getBirthday());
		System.out.println("e.code"+e.getCode());
		mapper.updateEmp(e);
		return s;		
	}
}
